package vn.ugame.model.bl;

import java.sql.SQLException;
import java.util.List;
import java.util.Locale;
import vn.ugame.exception.DataConnectionFailed;
import vn.ugame.exception.EntityNotFound;
import vn.ugame.exception.ExceptionCode;
import vn.ugame.exception.InvalidEntityClass;
import vn.ugame.model.dal.AccountDA;
import vn.ugame.model.dal.StoreDA;
import vn.ugame.model.entity.*;

public class StoreService {

    public StoreService() {
    }

    public FindResult<Item> findItems(String moduleId, String keyword,
            int page, Locale locale)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        int pageSize = 4;
        int fromIndex = (page - 1) * pageSize;
        int toIndex = page * pageSize;
        IntWapper count = new IntWapper();

        FindResult<Item> result = new FindResult<Item>();

        StoreDA storeDA = new StoreDA();

        result.setPage(page);
        result.setKeyword(keyword);

        List<Item> items = storeDA.searchItems(keyword, moduleId, locale, fromIndex, toIndex, count);
        result.setResult(items);
        result.setCount(count.getValue());

        int pageCount = 1;
        if (!items.isEmpty()) {
            pageCount = (int) Math.ceil((double) count.getValue() / pageSize);
        }
        result.setPageCount(pageCount);

        return result;
    }

    public Item findLocalItem(String id, Locale locale)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        StoreDA storeDA = new StoreDA();
        return storeDA.findLocalItem(id, locale);
    }

    public Item findLocalItem(String id, String name)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        StoreDA storeDA = new StoreDA();
        return storeDA.findLocalItem(id, name);
    }

    public Item findLocalItem(String moduleId, String id, String name)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        StoreDA storeDA = new StoreDA();
        List<Item> items = storeDA.findLocalItems(id, moduleId, null, null, null, name, null, null, null, null, null);
        if (!items.isEmpty()) {
            return items.get(0);
        }
        return null;
    }

    public List<Item> findLocalItems(String id)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        StoreDA storeDA = new StoreDA();
        return storeDA.findLocalItems(id, null, null, null, null, null, null, null, null, null, null);
    }

    public List<Item> findLocalRelatedItems(String id, Locale locale)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        StoreDA storeDA = new StoreDA();
        return storeDA.findLocalRelatedItems(id, locale);
    }

    public List<Item> findSliderLocalItems(String moduleId, Locale locale, Integer type, Integer top)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        StoreDA storeDA = new StoreDA();
        return storeDA.findSliderLocalItems(moduleId, locale, type, top);
    }

    public FindResult<Comment> findComments(String itemId, int page)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        int pageSize = 3;
        int fromIndex = (page - 1) * pageSize;
        int toIndex = page * pageSize;
        IntWapper count = new IntWapper();

        FindResult<Comment> result = new FindResult<Comment>();

        StoreDA storeDA = new StoreDA();

        result.setPage(page);
        result.setPageSize(pageSize);

        List<Comment> comments = storeDA.findComments(null, itemId, null, null, null, fromIndex, toIndex, count);
        result.setResult(comments);
        result.setCount(count.getValue());

        int pageCount = 1;
        if (!comments.isEmpty()) {
            pageCount = (int) Math.ceil((double) count.getValue() / pageSize);
        }
        result.setPageCount(pageCount);

        return result;
    }

    public List<Image> findImage(String itemId, int type)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        StoreDA storeDA = new StoreDA();
        return storeDA.findItemImages(null, itemId, type, null);
    }

    public void createDownloadItemLog(String itemId, String email)
            throws DataConnectionFailed, SQLException,
            InvalidEntityClass, EntityNotFound {

        AccountDA accountDA = new AccountDA();
        Account account = accountDA.findAccountByEmail(email);
        if (account == null) {
            throw new EntityNotFound(ExceptionCode.ACCOUNT_NOT_FOUND);
        }

        StoreDA storeDA = new StoreDA();
        storeDA.createDownloadItemLog(itemId, account.getId());
    }

    public void createComment(String itemId, String email, String content)
            throws DataConnectionFailed, SQLException,
            InvalidEntityClass, EntityNotFound {

        AccountDA accountDA = new AccountDA();
        Customer customer = accountDA.findCustomerByEmail(email);
        if (customer == null) {
            throw new EntityNotFound(ExceptionCode.ACCOUNT_NOT_FOUND);
        }

        StoreDA storeDA = new StoreDA();
        storeDA.createComment(itemId, customer.getId(), content);
    }
}
